#include "smoker.h"
#include<sys/wait.h>
int main() {
    init_semaphores();

    // 初始化共享数据
    int shmid = shmget(ftok(".", 'D'), sizeof(SharedData), 0666 | IPC_CREAT);
    SharedData *shared_data = (SharedData *)shmat(shmid, NULL, 0);
    shared_data->current_combo = 0;

    // 创建供应者和抽烟者进程
    for (int i = 0; i < NUM_SUPPLIERS; i++) {
        if (fork() == 0) {
            execl("./supplier", "supplier", (char[]){i + '0', 0}, NULL);
            exit(0);
        }
    }

    // 创建三个抽烟者进程
    for (int i = 0; i < NUM_SMOKERS; i++) {
        if (fork() == 0) {
            execl("./smoker", "smoker", (char[]){i + '0', 0}, (char[]){i + '0', 0}, NULL);
            exit(0);
        }
    }

    // 等待所有子进程
    for (int i = 0; i < NUM_SUPPLIERS + NUM_SMOKERS; i++) wait(NULL);

    // 清理资源
    semctl(mutex_sem, 0, IPC_RMID);
    for (int i = 0; i < NUM_SMOKERS; i++) semctl(smoker_sem[i], 0, IPC_RMID);
    semctl(supplier_sem, 0, IPC_RMID);
    shmdt(shared_data);
    shmctl(shmid, IPC_RMID, NULL);

    return 0;
}
